package h0829;

import h0828.ReverseList;

import java.util.List;

/**
  * @description 反转链表
  * @author 不知名帅哥
  * @date 2024/8/29 1:01
  * @version 1.0
*/
public class ReverseLinkedList {
    static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))));
        ListNode res = getRes(head);
        while (res != null) {
            System.out.println(res.val);
            res = res.next;
        }
    }

    private static ListNode getRes(ListNode head) {
        if (head==null) return null;
        ListNode dummy=new ListNode(-1);
        dummy.next=head;
        ListNode p=head;
        ListNode q=p.next;
        while (p.next!=null){
            p.next=q.next;
            q.next=dummy.next;
            dummy.next=q;
            q=p.next;
        }
        return dummy.next;
    }
}
